
function save() {
	if(!$("#name").val()){
        layer.alert("请填写用户组名称!");
        return;
	}
	if(overAllIds.length == 0){
        layer.alert("请选择用户!");
        return;
	}
	$.ajax({
		type : "POST",
		url : "/sys/userGroup/update",
		data : {
			"id": $("#idz").val(),
			"name": $("#name").val(),
			"userIds" : overAllIds.join(",")
		},
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg);
			}
		}
	});
}

//初始化
$(function(){
    var userOldIds = $("#userOldIds").val().toString();
    for(var i=0 ;i<userOldIds.split(",").length; i++){
        overAllIds.push(userOldIds.split(",")[i]);
    }
    appendHtml();
});

//全局数组
var overAllIds = [];

var selUser = function(){
    // iframe层
    var index = layer.open({
        type : 2,
        title : '选择用户',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '800px', '520px' ],
        content : '/sys/userGroup/treeView'
    });
    layer.full(index);
};


var appendHtml = function(){
    $("#ryId").html("");
    if(overAllIds.length != 0){
        $.ajax({
            type : "POST",
            url : "/sys/user/selectUserByIds",
            data : {
                "ids" : overAllIds
            },
            error : function(request) {
                parent.layer.alert("Connection error");
            },
            success : function(res) {
                $.each(res.data,function(index,valz){
                    var htmlz = "<div>"+valz.name+"<button type='button'  onclick='delUser(this)' id='"+valz.id+"'>移除</button></div>";
                    $("#ryId").append(htmlz);
                });
            }
        });
	}
};

var delUser = function (that) {
    for(var i=0 ;i<overAllIds.length; i++){
    	if( $(that).attr("id") == overAllIds[i]){
            overAllIds.splice(overAllIds.indexOf(overAllIds[i]),1);
		}
    }
    appendHtml();
};
